local Config = require("Config")
local HeroExcel = require("excel.hero").hero
local HeroAttrExcel = require("excel.hero").attr
local HeroSpecialExcel = require("excel.hero").specialHero
local PfLogic = require("platform.PfLogic")
local HeroGrid = require("hero.HeroGrid")

HERO_BAG_CNT = 150				 -- 背包默认的数值：60
HERO_BAG_BUY_ADD_CNT = 10		 -- 英雄背包每次购买增加5格
PAGE_HERO_COUNT = 30			 --英雄背包每次发送的数量
BUY_CAP_CNT = 40                 --玩家购买英雄容量的次数
JIHUO_SHUIJING_NEEDLV = 40	     --激活水晶所需等级
DEFAULT_SHUIJING_GET = 20011	 --激活后默认获得的装备ID
DEFAULT_SHENQI_UPLV_BACK = 165 --神器升级后的返还物ID
HERO_SKIN_CNT = 999

SHUIJING_UP_LV_LOCK = 2 -- 升级为原有水晶

WEIGHTLV2HEROS = {}
HEROELITE = {} --精英英雄列表
WEIGHTLV2CAMPHEROS = {}   --英雄道具碎片抽奖[权重][阵营]
ATTRHEROS = {}  -- 属性列表

-- 判断是否为英雄
function isHero(id)
	local heroConfig = HeroExcel[id]
	if not heroConfig then return end
	return id and HeroExcel[id]
end

-- 获得配置
function getConfig(id)
	if not id then return end
	return HeroExcel[id]
end

-- 获取英雄星级区间
function getAttrConfig(id, star)
   local config = getConfig(id)
   if not config then return end

   if not ATTRHEROS[id] then
      return HeroAttrExcel[0]
   end
   
   local attrID = ATTRHEROS[id][star]
   if not attrID then 
      --assert(nil, "getAttrConfig attrID is nil")
      return
   end

   local attrConfig = HeroAttrExcel[attrID]
   if not attrConfig then
      --assert(nil, "attrConfig is nil")
      return
   end

   return attrConfig
end

-- 获取下一星级 属性配置
function getNextAttrConfig(id, star)
   local config = getConfig(id)
   if not config then return end

   if not ATTRHEROS[id] then
      return HeroAttrExcel[0]
   end

   local attrID = ATTRHEROS[id][star]
   if not attrID then 
      return
   end
   --没有下一星级升星配置
   local attrConfig = HeroAttrExcel[attrID]
   local cond = #attrConfig.cond
   if cond <= 0 then return end

   star = star + 1
   attrID = ATTRHEROS[id][star]
   if not attrID then 
      return
   end
   attrConfig = HeroAttrExcel[attrID]

   return attrConfig
end


function initAfterHot()
	-- 初始化英雄属性列表
    ATTRHEROS = {}
	for k,v in pairs(HeroAttrExcel) do
        ATTRHEROS[v.heroID] = ATTRHEROS[v.heroID] or {}
        ATTRHEROS[v.heroID][v.star] = k
	end
    
    for i = 1,20 do
		WEIGHTLV2HEROS[i] = nil
	end


	HEROELITE = {}

    local len = 0
	for k, v in pairs(HeroExcel) do
		if v.weightLv > 0 then
			WEIGHTLV2HEROS[v.weightLv] = WEIGHTLV2HEROS[v.weightLv] or {}
			WEIGHTLV2HEROS[v.weightLv][#WEIGHTLV2HEROS[v.weightLv] + 1] = k

			WEIGHTLV2CAMPHEROS[v.weightLv] = WEIGHTLV2CAMPHEROS[v.weightLv] or {}
            WEIGHTLV2CAMPHEROS[v.weightLv][v.camp] = WEIGHTLV2CAMPHEROS[v.weightLv][v.camp] or {}

			WEIGHTLV2CAMPHEROS[v.weightLv][v.camp][#WEIGHTLV2CAMPHEROS[v.weightLv][v.camp] + 1] = k
		end
		
		if v.isElite == 1 and v.weightLv > 0 then
            len = len + 1
		    HEROELITE[len] = k
		end
	end
end

function getRandHeroByWeightLv(weightLv)
	if WEIGHTLV2HEROS[weightLv] then
		local r = math.random(1,#WEIGHTLV2HEROS[weightLv])
		return WEIGHTLV2HEROS[weightLv][r]
	end
end

function getRandHeroByWeightLvAndCamp(weightLv, camp)
    if camp == 0 then 
        return getRandHeroByWeightLv(weightLv)
    end

	if WEIGHTLV2CAMPHEROS[weightLv] then
        if WEIGHTLV2CAMPHEROS[weightLv][camp] then
            local r = math.random(1,#WEIGHTLV2CAMPHEROS[weightLv][camp])
		    return WEIGHTLV2CAMPHEROS[weightLv][camp][r]
        end
	end
end

function getHeroByWeightLvAndCamp(weightLv, camp)
    if camp == 0 then 
        return WEIGHTLV2HEROS[weightLv]
    end

    return WEIGHTLV2CAMPHEROS[weightLv][camp]
end

function getRandHeroByIsElite()
	if HEROELITE then
		local r = math.random(1,#HEROELITE)
		return HEROELITE[r]
	end
end

-- 获取图鉴属性最大值
local TUJIAN_KEY_2_MAXVALUE = nil
function getTujianMaxValue(key)
	if not TUJIAN_KEY_2_MAXVALUE then
		TUJIAN_KEY_2_MAXVALUE = {}
		for heroID in pairs(HeroSpecialExcel) do
			local heroGrid = HeroGrid.getCacheHeroGridTujian(heroID)
			if heroGrid and heroGrid.attrs then
				for k, v in pairs(heroGrid.attrs) do
					if v > (TUJIAN_KEY_2_MAXVALUE[k] or 0) then
						TUJIAN_KEY_2_MAXVALUE[k] = v
					end
				end
			end
		end
	end
	return TUJIAN_KEY_2_MAXVALUE[key] or 0
end